package com.jxs.android.ges;

import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jxs.android.util.HandlerHolder;
import com.jxs.android.util.MemoryUtil;
import com.jxs.android.util.StringUtils;
import com.jxs.android.util.Timer;
import com.jxs.android.weiget.BuAlertDailog;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.TopBar;

@SuppressLint("HandlerLeak")
public class WorkLogComment extends AbstractActivity {
	private BuLoadingDailog progressDialog;

	private Button sumbit;
	private EditText text;
	private WorkLogCommentHolder workLogCommentHolder;
	private String wl_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.bu_work_log_comment);
		sumbit = (Button) findViewById(R.id.sumbit);
		text = (EditText) findViewById(R.id.bu_more_feedback_contact_input);
		workLogCommentHolder = new WorkLogCommentHolder(handler, wl_id);

		progressDialog = new BuLoadingDailog(this);

		if (null != getIntent() && null != getIntent().getExtras()) {
			wl_id = getIntent().getExtras().getString("wl_id");
		}
		sumbit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (StringUtils.isEmpety(text.getText().toString())) {
					errorMsgPop("请填写点评内容！");
					return;
				}
				progressDialog.showLoading(R.string.load_and_wait);
				workLogCommentHolder.addWorkLogComment(GesApplication.getUserInfo().getTicket(), text.getText().toString());
			}
		});

	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.hindSpinnerBtn();
		topBarReturn(topBar);
		topBar.setTitle("点评");
	}

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (!MemoryUtil.isCurrentActivity(WorkLogComment.class)) {
				return;
			}
			if (null != progressDialog && progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			if (msg.what == HandlerHolder.What.ERROR_MSG) {
				errorMsgPop(msg.obj.toString());
			} else if (msg.what == HandlerHolder.What.TO_LOGIN) {
				errorMsgToLogin(msg.obj.toString());
			} else if (msg.what == HandlerHolder.What.WORK_LOG_COMMENT_RESLT_OK) {
				errorMsgPop("点评成功！", new BuAlertDailog.IPhoneAlertDailog() {
					@Override
					public void okLogic(Dialog dialog) {
						dialog.cancel();
						Intent intent = new Intent();
						Bundle bundle = new Bundle();
						bundle.putString("content", text.getText().toString());
						bundle.putString("staff_name", GesApplication.getUserInfo().getName());
						bundle.putString("comment_date", Timer.getSDFyyyy_MM_dd_N_HHmmss().format(new Date()));
						intent.putExtras(bundle);
						setResult(RESULT_OK, intent);
						finish();
					}

					@Override
					public String getOkText(Context context) {
						return getString(R.string.promp_confirm);
					}
				});
			}
		}

	};

}